package com.bstek.uflo.form.view.common.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.htmlparser.Node;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;

import com.bstek.dorado.view.View;
import com.bstek.dorado.view.widget.Component;
import com.bstek.dorado.view.widget.form.autoform.AutoForm;
import com.bstek.dorado.view.widget.form.autoform.AutoFormElement;
import com.bstek.dorado.view.widget.layout.CommonLayoutConstraint;
import com.bstek.dorado.web.DoradoContext;
import com.bstek.uflo.client.service.TaskClient;
import com.bstek.uflo.form.Constants;
import com.bstek.uflo.form.model.TableColumn;
import com.bstek.uflo.form.model.TableDefinition;
import com.bstek.uflo.form.view.common.AbstractComponentBuilder;
import com.bstek.uflo.model.ProcessDefinition;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.process.node.FormElement;
import com.bstek.uflo.process.node.TaskNode;
import com.bstek.uflo.process.security.Authority;
import com.bstek.uflo.service.ProcessService;

@org.springframework.stereotype.Component("com.bstek.uflo.form.view.common.ApproveAutoFormBuilder")
public class ApproveAutoFormBuilder extends AbstractComponentBuilder {
	
	@Autowired
	@Qualifier(TaskClient.BEAN_ID)
	private TaskClient taskClient;
	
	@Autowired
	@Qualifier(ProcessService.BEAN_ID)
	private ProcessService processService;
	
	public boolean support(TagNode node) {
		if(node instanceof TableTag){
			TableTag table=(TableTag)node;
			String css=table.getAttribute("class");
			if(css!=null && css.equals("ufloapproveform")){
				return true;
			}
		}
		return false;
	}

	public void build(TagNode node, View view, Map<String, Component> map,Map<String, Boolean> mappingMap, TableDefinition table) {
		TableTag tableTag=(TableTag)node;
		NodeList trList=tableTag.getChildren();
		if(trList.size()==0)return;
		Node trNode=null;
		for(int i=0;i<trList.size();i++){
			Node n=trList.elementAt(i);
			if(n!=null && n instanceof TableRow){
				trNode=n;
				break;
			}
		}
		NodeList tdList=trNode.getChildren();
		if(tdList.size()==0)return;
		org.htmlparser.tags.TableColumn td=null;
		for(int i=0;i<tdList.size();i++){
			Node n=tdList.elementAt(i);
			if(n!=null && n instanceof org.htmlparser.tags.TableColumn){
				td=(org.htmlparser.tags.TableColumn)n;
				break;
			}
		}
		Div div=null;
		NodeList divList=td.getChildren();
		for(int i=0;i<divList.size();i++){
			Node n=divList.elementAt(i);
			if(n!=null && n instanceof Div){
				div=(Div)n;
				break;
			}
		}
		if(div==null)return;
		String id=div.getAttribute("id");
		if(StringUtils.isEmpty(id))return;
		HttpServletRequest request=DoradoContext.getCurrent().getRequest();
		if(map.containsKey(id))return;
		AutoForm autoform=buildAutoForm(table,request.getParameter("processId"),request.getParameter("taskId"));
		if(autoform!=null){
			autoform.setId(Constants.PROCESS_TASK_FORM);
			autoform.setRenderOn("#"+id);
			autoform.setId(Constants.PROCESS_TASK_FORM);
			autoform.setLabelWidth(110);
			view.addChild(autoform);
			map.put(id, autoform);
		}
	}
	
	private AutoForm buildAutoForm(TableDefinition table,String processId,String taskId){
		if(StringUtils.isEmpty(processId) || StringUtils.isEmpty(taskId)){
			return null;
		}
		AutoForm form=new AutoForm();
		form.setCols("*,*");
		ProcessDefinition process=processService.getProcessById(Long.valueOf(processId));
		Task task=taskClient.getTask(Long.valueOf(taskId));
		TaskNode taskNode=(TaskNode)process.getNode(task.getNodeName());
		List<TableColumn> columns=table.getColumns();
		List<FormElement> formElements=taskNode.getFormElements();
		for(FormElement element:formElements){
			boolean exist=false;
			for(TableColumn col:columns){
				if(col.getName().equals(element.getName())){
					exist=true;
					break;
				}
			}
			if(exist)continue;
			AutoFormElement autoFormElement=new AutoFormElement();
			autoFormElement.setName(element.getName());
			autoFormElement.setLabel(element.getCaption());
			autoFormElement.setEditorType(element.getEditorType());
			autoFormElement.setReadOnly(element.getAuthority().equals(Authority.Read));
			form.addElement(autoFormElement);
		}
		AutoFormElement formElement=new AutoFormElement();
		formElement.setName(Constants.TASK_OPINION);
		formElement.setLabel("任务处理意见");
		formElement.setLayoutConstraint(buildLayoutConstraint("2", null));
		form.addElement(formElement);
		return form;
	}
	
	private CommonLayoutConstraint buildLayoutConstraint(String colSpan,String rowSpan){
		CommonLayoutConstraint commonLayoutConstraint = new CommonLayoutConstraint();
		if(StringUtils.hasText(colSpan)){
			commonLayoutConstraint.put("colSpan",colSpan);			
		}
		if(StringUtils.hasText(rowSpan)){
			commonLayoutConstraint.put("rowSpan", rowSpan);			
		}
		return commonLayoutConstraint;
	}
}
